package com.ruoyi.exam.controller;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.exam.domain.QuestionsType;
import com.ruoyi.exam.service.IQuestionsTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 试题类型Controller
 *
 * @author 王室
 * @date 2021-10-12
 */
@RestController
@RequestMapping("/system/type")
public class QuestionsTypeController extends BaseController
{
    @Autowired
    private IQuestionsTypeService questionsTypeService;

    /**
     * 查询试题类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(QuestionsType questionsType)
    {
        startPage();
        List<QuestionsType> list = questionsTypeService.selectQuestionsTypeList(questionsType);
        return getDataTable(list);
    }

    /**
     * 导出试题类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:export')")
    @Log(title = "试题类型", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(QuestionsType questionsType)
    {
        List<QuestionsType> list = questionsTypeService.selectQuestionsTypeList(questionsType);
        ExcelUtil<QuestionsType> util = new ExcelUtil<QuestionsType>(QuestionsType.class);
        return util.exportExcel(list, "试题类型数据");
    }

    /**
     * 获取试题类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(questionsTypeService.selectQuestionsTypeById(id));
    }

    /**
     * 新增试题类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:add')")
    @Log(title = "试题类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuestionsType questionsType)
    {
        // 获取登录人ID
        LoginUser loginUser = getLoginUser();
        Long userId = loginUser.getUserId();
        questionsType.setUserId(userId);
        questionsType.setDel("0");
        return toAjax(questionsTypeService.insertQuestionsType(questionsType));
    }

    /**
     * 修改试题类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "试题类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QuestionsType questionsType)
    {
        return toAjax(questionsTypeService.updateQuestionsType(questionsType));
    }

    /**
     * 删除试题类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:remove')")
    @Log(title = "试题类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(questionsTypeService.deleteQuestionsTypeByIds(ids));
    }
}
